﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DAL;
namespace BAL
{
    public class BAL_KHACHHANG
    {
        private static QuanLyTiemNetDataContext db = new QuanLyTiemNetDataContext();
        //Thêm
        public static bool ThemKH(ref string err,string MaKhach,string HoTen,string SDT,string MaLoaiKH)
        {
            bool them = false;
            try
            {
                KHACHHANG kh = new KHACHHANG();
                kh.MaKhach = MaKhach;
                kh.HoTen = HoTen;
                kh.SDT = SDT;
                kh.MaLoaiKH = MaLoaiKH;
                kh.DaXoa = false;
                db.KHACHHANGs.InsertOnSubmit(kh);
                db.SubmitChanges();
                them = true;
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return them;
        }

        //cập nhật
        public static bool CapNhatKH(ref string err,string MaKhach,string HoTen,string SDT,string MaLoaiKH)
        {
            bool update = false;
            try
            {
                KHACHHANG tim = db.KHACHHANGs.Single(kh => kh.MaKhach.Equals(MaKhach));
                if(tim!=null)
                {
                    tim.MaKhach = MaKhach;
                    tim.HoTen = HoTen;
                    tim.SDT = SDT;
                    tim.MaLoaiKH = MaLoaiKH;
                    db.SubmitChanges();
                    update = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return update;
        }
        //xóa
        public static bool XoaKH(ref string err,string MaKhach)
        {
            bool xoa = false;
            try
            {
                KHACHHANG tim = db.KHACHHANGs.Single(kh => kh.MaKhach.Equals(MaKhach));
                if (tim != null)
                {
                    db.KHACHHANGs.DeleteOnSubmit(tim);
                    db.SubmitChanges();
                    xoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return xoa;
        }

        //Đánh dấu xóa
        public static bool DanhDauXoa(ref string err,string MaKhach,bool value)
        {
            bool danhdauxoa = false;
            try
            {
                KHACHHANG tim = db.KHACHHANGs.Single(kh => kh.MaKhach.Equals(MaKhach));
                if (tim != null)
                {
                    tim.DaXoa = value;
                    db.SubmitChanges();
                    danhdauxoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;

            }
            return danhdauxoa;
        }
        //ConverToDataTable
        public static DataTable ConverToDataTable(List<KHACHHANG> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaKhach");
            dt.Columns.Add("HoTen");
            dt.Columns.Add("SDT");
            dt.Columns.Add("MaLoaiKH");
            //dt.Columns.Add("DaXoa");
            foreach(var i in list)
            {
                dt.Rows.Add(i.MaKhach, i.HoTen, i.SDT, i.MaLoaiKH);
            }
            return dt;
        }
        //Lấy thông tin
        public static DataTable LayThongTinKH()
        {
            
            var ttkh = from kh in db.KHACHHANGs where kh.DaXoa==false select kh;
            return ConverToDataTable(ttkh.ToList());
        }
        //lấy lịch sử xóa khách hàng
        public static DataTable LayLichSuKH()
        {

            var ttkh = from kh in db.KHACHHANGs where kh.DaXoa == true select kh;
            return ConverToDataTable(ttkh.ToList());
        }
        //Lấy chiết khấu
        public static float LayChietKhauKH(string MaKhachHang)
        {
            
            BANGCHIETKHAU bck=new BANGCHIETKHAU();
            bool ktloi = false;
            try
            {
                KHACHHANG kh = db.KHACHHANGs.Single(k => k.MaKhach.Equals(MaKhachHang));
                bck = db.BANGCHIETKHAUs.Single(ck => ck.MaLoaiKH.Equals(kh.MaLoaiKH)&& ck.DaHuy.Value==false);
                ktloi = false;

            }
            catch (Exception)
            {
                ktloi = true;
            }
            if (ktloi)
                return 0;
            return bck.ChietKhau.Value;
        }
    }
}
